package com.wave.wavesystem.business.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.wave.wavesystem.business.pojo.emums.menu.MenuEnableStatusEnum;
import com.wave.wavesystem.business.pojo.entity.MenuEntity;
import com.wave.wavesystem.business.pojo.vo.MenuVO;
import com.wave.wavesystem.business.service.MenuService;

import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.EnumUtil;
import com.wave.wavesystem.common.util.Func;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import wave.common.pojo.entity.KV;
import wave.common.pojo.entity.Query;
import wave.common.pojo.entity.Result;
import wave.common.pojo.enums.RStatusEnum;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-01-22
 * @description 菜单表 访问层
 */
@RestController
@Tag(name = "菜单管理接口")
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 新增
     *
     * @param menu 新增实体
     */
    @Operation(summary = "新增")
    @PostMapping("/save")
    public Result<MenuEntity> save(@RequestBody MenuEntity menu) {
        menuService.save(menu);
        return Result.data(menuService.getById(menu.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(@RequestParam String ids) {
        List<String> idList = Func.strToStringList(ids);
        return Result.status(menuService.removeByIds(idList), RStatusEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param menu 更新内容
     */
    @Operation(summary = "更新")
    @PostMapping("/update")
    public Result<MenuEntity> update(@RequestBody MenuEntity menu) {
        menuService.updateById(menu);
        return Result.data(menuService.getById(menu.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param menu 查询条件
     */
    @Operation(summary = "查询详情")
    @GetMapping("/detail")
    public Result<MenuEntity> detail(MenuEntity menu) {
        return Result.data(menuService.getById(menu.getId()));
    }

    /**
     * 列表查询
     *
     * @param menu 查询条件
     */
    @Operation(summary = "列表查询")
    @GetMapping("/list")
    public Result<List<MenuEntity>> list(MenuEntity menu) {
        return Result.data(menuService.list(Condition.getQueryWrapper(menu)));
    }

    /**
     * 条件分页查询
     *
     * @param menu  查询条件
     * @param query 分页条件
     */
    @Operation(summary = "条件分页查询")
    @GetMapping("/page")
    public Result<Page<MenuEntity>> page(MenuEntity menu, Query query) {
        return Result.data(menuService.page(new Page<>(query.getPageNum(), query.getPageSize()), Condition.getQueryWrapper(menu)));
    }

    /**
     * 获取所有菜单
     */
    @Operation(summary = "获取所有菜单")
    @GetMapping("/allManu")
    public Result<List<MenuVO>> allMenu(String type) {
        return Result.data(menuService.allMenu(type));
    }

    /**
     * 获取顶部菜单
     */
    @Operation(summary = "获取顶部菜单")
    @GetMapping("/topMenu")
    public Result<List<KV<String, String>>> topMenu(String type) {
        return Result.data(menuService.topMenu(type));
    }

    /**
     * 获取子菜单
     *
     * @param parentId 父级菜单id
     */
    @Operation(summary = "获取子菜单")
    @PostMapping("/childrenMenu")
    public Result<List<MenuVO>> menuChildren(@RequestParam String parentId) {
        return Result.data(menuService.childrenMenu(parentId));
    }

    /**
     * 获取菜单启用状态枚举
     */
    @Operation(summary = "获取菜单启用状态枚举")
    @GetMapping("/menuEnableStatus")
    public Result<List<KV<String, Integer>>> menuEnableStatus() {
        List<KV<String, Integer>> menuStatus = EnumUtil.getKVAll(List.of(MenuEnableStatusEnum.values()),
                MenuEnableStatusEnum::getText, MenuEnableStatusEnum::getValue);
        return Result.data(menuStatus);
    }


    /**
     * 获取注册菜单
     */
    @Operation(summary = "获取注册菜单")
    @GetMapping("/getRegisterMenu")
    public Result<List<MenuEntity>> getRegisterMenu(@RequestParam String type) {
        return Result.data(menuService.list(QueryChain.of(MenuEntity.class)
                .eq(MenuEntity::getType, type)));
    }

}